<template>
  <div>
    <el-card class="oneCard" @click.native="jump">
      <div>
        <div>
          <el-link :type="type" :underline="false">{{ total }}</el-link>
          <h2>{{ name }}</h2>
        </div>
        <div><svg-icon :icon-class="icon" /></div>
      </div>
    </el-card>
  </div>
</template>
<script>
// import { mapGetters } from 'vuex'
import { assetTotalStats } from '@neu/dcam-ui/api/fixedAsset'
import { getInfo } from '@neu/biz-core/api/user'
import { detail } from '@neu/biz-core/api/role'
import { list } from '@neu/biz-core/api/role'
import { getToken } from '@neu/biz-core/utils/auth'
export default {
  name: 'FixedAssetStats',
  props: {
    belongs: {
      required: false,
      type: String,
      default: ''
    },
    categories: {
      required: false,
      type: String,
      default: ''
    },
    name: {
      required: false,
      type: String,
      default: '固定资产'
    },
    icon: {
      required: false,
      type: String,
      default: 'fixed'
    }
  },
  data() {
    return {
      type: 'warning',
      total: '--',
      roles: [],
      totalRoles: [],
      role: {},
      permissions: [],
      totalPermissions: [],
      hasPermissions: Boolean
    }
  },
  // computed: {
  //   ...mapGetters([
  //     'roles'
  //   ])
  // },
  created() {
    this.getTotal()
    getInfo(getToken()).then(response => {
      this.roles = response.data.roles
    })
    this.hasPermissions = false

    list().then(response => {
      this.totalRoles = response.data
    })
  },
  methods: {
    // 获取数量
    getTotal() {
      const params = {}
      if (this.belongs) {
        params.s_belongs = this.belongs
      }
      if (this.categories) {
        params.s_categories = this.categories
      }
      assetTotalStats(params).then(res => {
        this.total = res.data ? res.data : 0
      })
      this.total = 0
    },
    async jump() {
      let routerName = ''
      if (this.belongs === 'kj') {
        routerName = this.categories === '1' ? 'menu_kj_standingbook_fixedAssetMaintenance' : 'menu_kj_standingbook_lowvaluegoods'
      } else if (this.belongs === 'sl') {
        routerName = this.categories === '1' ? 'menu_sl_standingbook_fixedAssetMaintenance' : 'menu_sl_standingbook_lowvaluegoods'
      }

      for (var i = 0; i < this.roles.length; i++) {
        for (var j = 0; j < this.totalRoles.length; j++) {
          if (this.totalRoles[j].code === this.roles[i].role) {
            await detail(this.totalRoles[j].id).then(response => {
              this.totalPermissions = this.totalPermissions.concat(response.data.permissions)
            })
            break
          }
        }
      }

      for (var x = 0; x < this.totalPermissions.length; x++) {
        if (this.totalPermissions[x].code === routerName) {
          this.hasPermissions = true
        }
      }

      if (!this.hasPermissions) {
        if (this.belongs === 'kj') {
          routerName = 'menu_kj_assets_myAssetMaintenance'
        } else if (this.belongs === 'sl') {
          routerName = ''
        }
      }

      if (routerName === 'menu_kj_standingbook_fixedAssetMaintenance') {
        routerName = 'standingbook_kj_fixedAsset'
      } else if (routerName === 'menu_kj_standingbook_lowvaluegoods') {
        routerName = 'standingbook_kj_lowvaluegoods'
      } else if (routerName === 'menu_sl_standingbook_fixedAssetMaintenance') {
        routerName = 'standingbook_sl_fixedAsset'
      } else if (routerName === 'menu_sl_standingbook_lowvaluegoods') {
        routerName = 'standingbook_sl_lowvaluegoods'
      } else if (routerName === 'menu_kj_assets_myAssetMaintenance') {
        routerName = 'standingbook_kj_myAsset'
      }

      if (routerName) {
        this.$router.push({ name: routerName })
      }
    }
  }
}
</script>
